package com.zhongyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyu.mapper.LogMapper;
import com.zhongyu.mapper.OrderDetailMapper;
import com.zhongyu.pojo.Log;
import com.zhongyu.pojo.OrderDetail;
import com.zhongyu.pojo.User;
import com.zhongyu.service.LogService;
import com.zhongyu.vo.LogVo;
import com.zhongyu.vo.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Service("logService")
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {
@Resource
    private Log log;
    @Resource
    private LogMapper logMapper;


    @Override
    public void add(OrderDetail orderDetail, HttpSession session) {
        User loginUser= (User) session.getAttribute("loginUser");
        logMapper.insertCai("商品名称:"+orderDetail.getGoodName()+"数量:"+orderDetail.getNumber()+"单价:"+orderDetail.getPrice(),orderDetail.getYuanYin(),log.setType(Log.TYPE_CAI),loginUser.getId(),LocalDateTime.now());

    }
    @Override
    public PageResult searchAll(Integer pageNo, Integer pageSize, LogVo logVo) {
        LambdaQueryWrapper<Log> wrapper= Wrappers.<Log>lambdaQuery();
        wrapper.eq(logVo.getType()!=null,Log::getType,logVo.getType())
                .eq(logVo.getUserId()!=null,Log::getUserId,logVo.getUserId())
                .ge(logVo.getStartDate()!=null,Log::getCreatetime,logVo.getStartDate())
                .le(logVo.getEndDate()!=null,Log::getCreatetime,logVo.getEndDate());
        Page page=new Page(pageNo,pageSize);
        List<Log> list=logMapper.findAll(page,wrapper);
        return new PageResult(list,page.getTotal());
    }
    @Override
    public List<Log> searchAll() {
        return logMapper.selectList(null);
    }
}
